//
//  TitleTypeView.m
//  Singapore
//
//  Created by 薛泽军 on 15/5/8.
//  Copyright (c) 2015年 薛泽军. All rights reserved.
//

#define WINDOW [[UIApplication sharedApplication].delegate window]
#define WIDTH(view) view.frame.size.width
#define HEIGHT(view) view.frame.size.height
#define X(view) view.frame.origin.x
#define Y(view) view.frame.origin.y


#import "TitleTypeView.h"
@implementation TitleTypeView
{
    UILabel *_headViewLabel;
    UIScrollView *_scrollView;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(instancetype)initWithFrame:(CGRect)frame WithBtuName:(NSArray *)nameArray WithScrollView:(UIScrollView *)scrollView;
{
    self=[super initWithFrame:frame];
    if (self) {
        for (int i=0;i<nameArray.count;i++) {
            UIButton *but=[UIButton buttonWithType:UIButtonTypeCustom];
//            but.backgroundColor = [UIColor whiteColor];
            but.frame=CGRectMake(WIDTH(WINDOW)/nameArray.count*i,0,WIDTH(WINDOW)/nameArray.count,40);
            but.backgroundColor=[UIColor clearColor];
            [but setTitleColor:ComColor forState:UIControlStateSelected];
            [but setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [but setTitle:nameArray[i] forState:UIControlStateNormal];
            [but setTitle:nameArray[i] forState:UIControlStateSelected];
            but.tag=10+i;
            but.titleLabel.font=[UIFont systemFontOfSize:14];
            [but addTarget:self action:@selector(headBtuClick:) forControlEvents:UIControlEventTouchUpInside];
            if (i==0) {
                but.selected=YES;
            }
            UILabel *lable=[[UILabel alloc]initWithFrame:CGRectMake(X(but)+WIDTH(but),5,1,30)];
            lable.backgroundColor=[UIColor blackColor];
            lable.alpha=0.2;
            [self addSubview:lable];
            [self addSubview:but];
        }
        _headViewLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 40-1,WIDTH(WINDOW)/nameArray.count-20,3)];
        _headViewLabel.backgroundColor=ComColor;
        [self addSubview:_headViewLabel];
    
    }
    return self;
}
- (void)headBtuClick:(UIButton *)btu
{

    [self headBtuAnimateWith:(int)btu.tag];
}
- (void)headBtuAnimateWith:(int)tag
{
    UIButton *btu=(UIButton *)[self viewWithTag:tag];
    for (UIButton *but in self.subviews) {
        if ([but isKindOfClass:[UIButton class]]&&but.tag<20) {
            but.selected=NO;
            but.userInteractionEnabled=YES;
        }
    }
    self.myBlock(tag-10);
    btu.selected=!btu.selected;
    btu.userInteractionEnabled=NO;
//    NSLog(@"%ld",(long)btu.tag);
    [_scrollView setContentOffset:CGPointMake(WIDTH(_scrollView)*(btu.tag-10),0) animated:YES];
    [UIView animateWithDuration:0.3 animations:^{
        CGRect frame=_headViewLabel.frame;
        frame.origin.x=10+(btu.tag-10)*WIDTH(btu);
        _headViewLabel.frame=frame;;
    }];
}
- (void)scollViewDidEnd:(UIScrollView *)scollView;
{
    if ([scollView isKindOfClass:[UIScrollView class]]) {
        [self headBtuAnimateWith:scollView.contentOffset.x/WIDTH(self)+10];
    }
}
- (void)btuSelectWith:(void(^)(int tag))tag;
{
    self.myBlock=tag;
}
@end
